<?php 
/**
 * @author admin888 <doan.hut@gmail.com>
 * Description : Modle xử lý các thao tác tạo thư mẫu  
 * Date : 06/06/2011
 * 
 */
 class Model_Thumau extends Zend_Db_Table
 {
    protected  $_db ;
    protected  $_dmtm = "dm_loai_thu";
    protected  $_tm = "thu_mau";
    protected  $_tv = "thanhvien";
    protected  $_ch = "cau_hinh_user";
	/**
     * Kết nối database
     */ 
    public function __construct()
	{
	  $this->_db =  Zend_Registry::get('connectDb');
	}
    
    /**
     * Lấy list danh mục thư Mẫu 
     */
    public function getdmthumau()
    {
        $sql = $this->_db->select()
                         ->from($this->_dmtm)
                         ->where('su_dung = ?',1)
                         ->order('sap_xep ASC ');
        $query = $this->_db->fetchAll($sql);
      return $query;                         
                         
    }
    /**
     *@author admin888<doan.hut@gmail.com >
     * $data du lieu insert
     * Description : admi
     */
    public function themthumau($data)
    {
        $date = date('m-d-Y H:i:s');
        $array = array(
                    'uid'          => $data['idnddn'],
                    'ten_tm'       => $data['tenthumau'],
                    'loai_tm'      => $data['id_loai_thu'],
                    'noi_dung_tm'  => stripslashes($data['noidung_thu']),
                    'ngay_tao'     => $date,
                    'ngay_sua'     => $date,
                    'nguoi_sua'    => $data['idnddn']
                     
         );
         $query = $this->_db->insert($this->_tm,$array);
       return $query;  
    }
    /**
     *@author admin888 <doan.hut@gmail.com>
     * Description Lấy danh sach thu mau  
     */
    public function getallthumau($iddn)
    {	

    	$sql = $this->_db->select()
                       ->from($this->_tm)
                       ->join($this->_dmtm,$this->_dmtm.'.id = '.$this->_tm.'.loai_tm','ten_loai_thu')
                       ->join($this->_tv,$this->_tv.'.uid = '.$this->_tm.'.uid','name')             
                       ->where($this->_tm.'.uid = ?',$iddn);       
      $query = $this->_db->fetchAll($sql); 
     return $query;
                       
    }
    /**
     * admin888<doan.hut@gmail.com>
     * Enter description : Tim kiêm thu theo từ khóa
     * @param unknown_type $iddn
     * @param unknown_type $tukhoa
     */
    public  function getallthumautheotukhoa($iddn,$tukhoa)
    {
       $sql = $this->_db->select()
                       ->from($this->_tm)
                       ->join($this->_dmtm,$this->_dmtm.'.id = '.$this->_tm.'.loai_tm','ten_loai_thu')
                       ->join($this->_tv,$this->_tv.'.uid = '.$this->_tm.'.uid','name')             
                       ->where($this->_tm.'.uid = ?',$iddn)
                       ->where('ten_tm LIKE ?',$tukhoa.'%');       
      $query = $this->_db->fetchAll($sql); 
      
     return $query;	
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * Enter description : tìm kiếm thu mẫu theo chủng loại
     * @param unknown_type $cltm
     */
    public function getallthumautheocltm($iddn,$cltm)
    {
      $sql = $this->_db->select()
                       ->from($this->_tm)
                       ->join($this->_dmtm,$this->_dmtm.'.id = '.$this->_tm.'.loai_tm','ten_loai_thu')
                       ->join($this->_tv,$this->_tv.'.uid = '.$this->_tm.'.uid','name')             
                       ->where($this->_tm.'.uid = ?',$iddn)
                       ->where($this->_tm.'.loai_tm = ?',$cltm);       
      $query = $this->_db->fetchAll($sql); 
      
     return $query;	    	
    	
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * Enter description : tìm kiếm chính xác theo cả ten và chung laoi
     * @param unknown_type $iddn
     * @param unknown_type $cltm
     * @param unknown_type $tukhoa
     */
    public function getallthumautheocltmvatukhoa($iddn,$cltm,$tukhoa)
    {      
      $sql = $this->_db->select()
                       ->from($this->_tm)
                       ->join($this->_dmtm,$this->_dmtm.'.id = '.$this->_tm.'.loai_tm','ten_loai_thu')
                       ->join($this->_tv,$this->_tv.'.uid = '.$this->_tm.'.uid','name')             
                       ->where($this->_tm.'.uid = ?',$iddn)
                       ->where($this->_tm.'.loai_tm = ?',$cltm)
                       ->where('ten_tm LIKE ?',$tukhoa.'%');      
      $query = $this->_db->fetchAll($sql); 
      
     return $query;	   
    	
    	
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * update trang thái sử dụng khong su dung thu mau 
     */  
    public  function sudungthumau($data)
    {
    	$date = date('m-d-Y H:i:s');
    	$arr = array(
    		'su_dung'		=>$data['thongso'],
    		'nguoi_sua'		=>$data['uid'],
    		'ngay_sua'		=>$date
    	);
    	$where = "id_tm = ".$data['id'];
    	$query = $this->_db->update($this->_tm,$arr,$where);
    	
    	return $query;
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * Enter description :xoa thu mẫu
     * @param unknown_type $idtm
     */
    public function deletethumau($idtm)
    {
    	$where = 'id_tm ='.$idtm;
    	$xoa = $this->_db->delete($this->_tm,$where);
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * Enter description :xóa nhiều sản phẩm
     * @param unknown_type $mangid
     */
    public function deletealltm($mangid)
    {
    	$arrayid = explode(',', $mangid);
    	for ($i =0;$i< count($arrayid);$i++){
    		$idtm = $arrayid[$i];
    		//sql xóa
    		$where = 'id_tm = '.$idtm;
    		$xoa = $this->_db->delete($this->_tm,$where);
    		
    	}
    }
     /**
     * admin888 <doan.hut@gmail.com>
     * Enter description : Lay thu mau theo id
     * @param unknown_type $idtm
     */
    public function getthumau($idtm)
    {
    	$sql = $this->_db->select()
    					 ->from($this->_tm)
    					 ->join($this->_tv, $this->_tv.'.uid ='.$this->_tm.'.uid','name')
    					 ->join($this->_ch, $this->_ch.'.uid = '.$this->_tm.'.uid')
    					 ->where('id_tm = ?',$idtm);
    	$query = $this->_db->fetchRow($sql);
     return $query;			 
    			
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * update thu mau 
     */
    public function updatethumau($data)
    {
    	if ($data['sudung'] == 'on'){
    		$sd = 1;
    	}else {
    		$sd = 0;
    	}
    	$date = date('m-d-Y H:i:s');
    	$arr = array(
    		'ten_tm'		=> $data['tenthumau'],
    		'loai_tm' 		=>$data['id_loai_thu'],
    		'noi_dung_tm'	=>stripslashes($data['noidung_thu']),
    		'ngay_sua'		=>$date,
    		'nguoi_sua'     =>$data['idnddn'],
    		'su_dung'       =>$sd
    	);
    	$where = 'id_tm = '.$data['idtm'];
    	$query = $this->_db->update($this->_tm, $arr,$where);
     return $query;	
    }
    /**
     * 
     *admin888 <doan.hut@gmail.com>
     * Enter description : count id thu mau 
     * @param unknown_type $uid
     */
    public function demthuaum($uid)
    {
    	$sql = $this->_db->select()
    					 ->from($this->_tm,array('COUNT(*) AS tongthumau'))
    					 ->where($this->_tm.'.uid =?',$uid);
    	$query = $this->_db->fetchOne($sql);
    return $query;					 
    }
    
         
 }
?>
